
import { typeClassName } from '@/pages/config/classNameMap';
export default{
    methods: {
        loadPage () {
            let filter = {
                className: this.className,
                type: this.type
            }
            let newFilter = '';
            if (this.tools.isReal(filter)) {
                newFilter = this.classObject.util.getFilter(filter);
            }
            this.loading = true;
            return this.classObject.query(newFilter).then((flag) => {
                if (flag) {
                    this.loading = false;
                } else {
                    this.loading = false;
                }
                if (this.classObject.isValidHier()) {
                    this.basicData = this.classObject.record.hier[0].basic.map(d => {
                        return {
                            ...d,
                            ...d.values
                        }
                    });
                }else{
                    this.basicData = [];
                }
            });
        },
        // 获取下拉框
        getOptions (filter, classObject) {
            let newFilter = '';
            if (this.tools.isReal(filter)) {
                newFilter = classObject.util.getFilter(filter);
            }
            let statusOptions = []
            return classObject
                .query(newFilter)
                .then((flag) => {
                    if (flag) {
                        statusOptions = classObject.record.hier[0].basic.map(d => {
                            return {
                                label: d.title === null ? '' : d.title,
                                value: d.name === null ? '' : d.name
                            }
                        })
                    } else {
                        statusOptions = [];
                    }
                    return statusOptions;
            });
        },
        async deleteRecord (names,selection) {
            let pointer = { className: this.className, type: this.type }
            //是否有删除条件且满足
            if(typeof(this.deleteCondition)==='function'&&!await this.deleteCondition(selection)){
                this.loading = false;
                return Promise.resolve()
            }
            // 解绑hier中某项的接口
            this.loading = true;
            this.classObject
                .runRemoveRegister(pointer, names)
                .then((flag) => {
                    if (flag) {
                        this.loading = false;
                        names.forEach((name) => {
                            let index = this.basicData.findIndex(
                                (d) => d.name === name
                            );
                            if (index !== -1) {
                                this.basicData.splice(index, 1);
                            }
                        });
                    } else {
                        this.loading = false;
                    }
            });
        },
        // 查询，有参数时前端过滤查询
        handleSearch (filter) {
            if (!this.isEmpty(filter)) {
                this.basicData = this.handleQueryNode(filter);
            } else {
                this.pageNavigation.pageNumber = 1;
                this.loadPage();
            }
        },
        handleReset () {
            this.pageNavigation.pageNumber = 1;
            this.loadPage();
        },
        isEmpty (filter) {
            let flag = true;
            for (let key in filter) {
                if (filter[key] !== '') {
                    flag = false
                }
            }
            return flag;
        },
        handleQueryNode (filter) {
            let basicData = [];
            if (
                !!this.classObject.record &&
                !!this.classObject.record.hier &&
                !!this.classObject.record.hier[0]
            ) {
                basicData = this.classObject.record.hier[0].basic.filter(d => {
                    let flag = true;
                    for (let key in filter) {
                        if (d.values[key].indexOf(filter[key])===-1) {
                            flag = false;
                        }
                    }
                    return flag;
                })
                return basicData.map(d => {
                    return { ...d, ...d.values };
                })
            }
        },
        handleCreate () {
            this.$set(this.addNewPanel.drawer, 'show', true);
        },
        async handleNewAddSubmit (data) {
            let pointer = { className: this.className, type: this.type }
            //是否有新增确认条件且满足
            if(typeof(this.addCondition)==='function'&&!await this.addCondition(data)){
                return;
            }
            let newData = {
                values: data.values,
                name: this.tools.uuid(8),
                id: this.tools.uuid(8),
                model: this.modelType,
                pointer: {}
            }
            // 添加hier中某项的接口
            this.loading = true;
            this.classObject
                .runAddRegister(pointer, { basic: [newData] })
                .then((flag) => {
                    if (flag) {
                        this.loading = false;
                        this.basicData.push({...newData,...newData.values});
                        this.$set(this.addNewPanel.drawer, 'show', false);
                    } else {
                        this.loading = false;
                    }
            });
        },
        // 打开编辑弹窗
        async handleEdit (row, index) {
            this.loading = true;
            await this.loadPage();
            if (this.classObject.isValidHier()) {
                let index = this.classObject.record.hier[0].basic.findIndex(
                    (d) => d.name === row.name
                );
                if (index !== -1) {
                    this.$set(this.editPanel, 'data', this.classObject.record.hier[0].basic[index]);
                    this.$set(this.editPanel.drawer, 'show', true);
                }
                this.loading = false;
            } else {
                this.loading = false;
            }
        },
        handleEditSubmit (data) {
            let pointer = { type: this.type, className: this.className };
            // 更新hier中某项的接口
            this.loading = true;
            this.classObject.runRegister(pointer, { basic: [data] }).then((flag) => {
                if (flag) {
                    let index = this.basicData.findIndex(
                        (d) => d.name === data.name
                    );
                    if (index !== -1) {
                        this.basicData.splice(index, 1, {...data,...data.values});
                    }
                    this.loading = false;
                    this.$set(this.editPanel.drawer, 'show', false);
                } else {
                    this.loading = false;
                }
            });
        },
        handleDetail (row, index) {
            //是否有查看数据处理
            let detailData = this.tools.lodash.cloneDeep(row)
            if(typeof(this.readDataCondition)==='function'){
                this.readDataCondition(detailData);
            }
            this.$set(this.readPanel, 'data', detailData);
            this.$set(this.readPanel.drawer, 'show', true);
        },
        handleDelete (row, index) {
            let _this = this;
            this.$Modal.confirm({
                title: this.lang['confirmDelete'],
                cancelText: this.lang['cancel'],
                onOk () {
                    _this.deleteRecord([row.name],[row]);
                }
            })
        },
        async handleDeleteMultiple (selection) {
            let _this = this;
            this.$Modal.confirm({
                title: this.lang['confirmDelete'],
                cancelText: this.lang['cancel'],
                async onOk () {
                    let names = [];
                    selection.map((item) => names.push(item.name));
                    await _this.deleteRecord(names,selection).then((flag) => {
                        if (flag) {
                            _this.$refs.table.selection = [];
                        }
                    });
                }
            })
        },
        getOptionValue (options, value, key='label') {
            let dataValue = ''
            let index = options.findIndex(d => d.value === value)
            if (index !== -1) {
                dataValue = options[index][key];
            }
            return dataValue;
        },
        getOptionValueByArray(options, valueArray, key='label') {
            if (!!valueArray && Array.isArray(valueArray)) {
              let dataValue = ''
              valueArray.forEach(value => {
                  let index = options.findIndex(d => d.value === value)
                  if (index !== -1) {
                      dataValue += `${options[index][key]},`;
                  }
              })
              return dataValue;
            }
        },
        //通过级联获取数据
        getOptionValueByCascader(options, valueArray,key='label') {
            if (!!valueArray && Array.isArray(valueArray)) {
              let dataValue = ''
              valueArray.forEach(value => {
                  let index = options.findIndex(d => d.value === value)
                  if (index !== -1) {
                    let childValue = this.getOptionValueByCascader(options[index].children,valueArray,key)
                    dataValue += `${options[index][key]}/`+childValue;
                  }
              })
              return dataValue;
            }
        },
        /**
         * 功能说明：通过维护页面的数据获取下拉框，比如设备类型下拉框
         * classMapName:classNameMap.js中对照表
         * classObject：classObject
         * optionName：定义的下拉框数组名称
         * optionConfig：返回的hier取值对应的字段，比如label对应的sf_name,value对应的sf_code
         * optionConfig = {label:'sf_name',value:'sf_code'}
         */
        setOptionsByHier (classMapName, classObject, optionName, optionConfig = { label: 'sf_name', value: 'sf_code' }) {
            let filter = {
                className: typeClassName[classMapName].className,
                type: typeClassName[classMapName].type
            }
            let newFilter = '';
            if (this.tools.isReal(filter)) {
                newFilter = classObject.util.getFilter(filter);
            }
            let statusOptions = []
            return classObject
                .query(newFilter)
                .then((flag) => {
                    if (flag) {
                        statusOptions = classObject.record.hier[0].basic.map(d => {
                            let option = {};
                            for (let key in optionConfig) {
                                option[key] = d.values[optionConfig[key]]
                            }
                            return option;
                        })
                    } else {
                        statusOptions = [];
                    }
                    this[optionName] = statusOptions
            });
        },
    }
}
